<template>
  <div class="home">
    <h1>购物车</h1>
    <div v-for="product in products" :key="product.id" class="product">
      <div class="product-info">
        <span class="product-name">{{ product.name }}</span>
        <span class="product-price">¥{{ product.price }}</span>
      </div>
      <div class="quantity-control">
      数量：
        <button 
          @click="decrementQuantity(product.id)" 
          :disabled="product.quantity <= 0"
        >-</button>
        <span class="quantity">{{ product.quantity }}</span>
        <button @click="incrementQuantity(product.id)">+</button>
      </div>
    </div>
    
    <div class="summary">
      <div>已选商品: {{ cartItemCount }}件</div>
      <div class="total">总价: ¥{{ cartTotal }}</div>
    </div>
  </div>
</template>

<script>
import { mapGetters, mapActions } from 'vuex'

export default {
  name: 'HomeView',
  computed: {
    ...mapGetters(['products', 'cartTotal', 'cartItemCount'])
  },
  methods: {
    ...mapActions(['incrementQuantity', 'decrementQuantity'])
  }
}
</script>

<style>
.home {
  max-width: 800px;
  margin: 0 auto;
  padding: 20px;
}

.product {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 15px;
  margin: 10px 0;
  border: 1px solid #eee;
  border-radius: 5px;
}

.product-info {
  display: flex;
  flex-direction: column;
}

.product-name {
  font-weight: bold;
  margin-bottom: 5px;
}

.product-price {
  color: #e53935;
}

.quantity-control {
  display: flex;
  align-items: center;
}

.quantity-control button {
  width: 30px;
  height: 30px;
  font-size: 16px;
  border: 1px solid #ddd;
  background: #f5f5f5;
  cursor: pointer;
}

.quantity-control button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.quantity {
  margin: 0 10px;
  min-width: 20px;
  text-align: center;
}

.summary {
  margin-top: 30px;
  padding: 15px;
  background: #f9f9f9;
  border-radius: 5px;
}

.total {
  font-size: 1.2em;
  font-weight: bold;
  margin-top: 10px;
  color: #e53935;
}
</style>